import cron from 'node-cron';
import db from './mysqlHelper.js';
import logger from './logger.js';
import moment from 'moment';
// import redis from './redis.js';

//每天凌晨1点生成日报表
cron.schedule('0 1 * * *', async () => {

});

async function sleep(ms) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
}

//每十分钟执行一次超时检查
cron.schedule('*/10 * * * *', async () => {

    //获取超时配置
    let sql = `select * from configuration where config_no=1 `;
    let configs = await db.query(sql);
    if (configs == null) {
        logger.error(`configuration not found`);
        return;
    }
    let timeout = parseInt(configs[0].config_val);
    if (isNaN(timeout)) {
        logger.error(`configuration error`);
        return;
    }
    let timeout_time = moment().subtract(timeout, 'hour').format('YYYY-MM-DD HH:mm:ss');
    //查询超时的运单
    sql = `UPDATE transport_record 
           SET timeout_status = 1 
           WHERE source_create_time <= '${timeout_time}' 
           AND (status = 0 OR status = 1 OR status = 2)`;

    let result = await db.query(sql);
    if (result instanceof Error) {
        logger.error(`运单超时更新检查执行失败 :` + result.message);
        return;
    }
    console.log('运单超时更新检查执行成功');
});



